package com.testapp.bluetoothtest08.Dialog;


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Looper;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSONObject;
import com.testapp.bluetoothtest08.Constant.Constants;
import com.testapp.bluetoothtest08.File.Statement;
import com.testapp.bluetoothtest08.Http.OkHttp;
import com.testapp.bluetoothtest08.Judge.IsRed;
import com.testapp.bluetoothtest08.R;

import java.util.zip.Inflater;

public class DiagnosedDialog extends Dialog {
    private Button diagnosed_confirm, diagnosed_cancel;
    private EditText confirm;
    private String code;
    private View v;
    private TextView statement;
    private OkHttp http = new OkHttp();

    public DiagnosedDialog(@NonNull Context context, View view) {
        super(context);
        this.v = view;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.diagnosed);
        init();
    }

    private void init() {
        diagnosed_cancel = findViewById(R.id.diagnosed_cancel);
        diagnosed_confirm = findViewById(R.id.diagnosed_confirm);
        confirm = findViewById(R.id.confirmCode);

        statement = v.findViewById(R.id.my_statement);

        diagnosed_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        diagnosed_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        code = confirm.getText().toString();
                        if(code.equals("")){
                            code = "0";
                        }
                        int c = Integer.parseInt(code);
                        new IsRed().judge(c);

                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        statement.setText(new Statement().read());
                        String s= new Statement().read();
                        if(s.equals("确诊者")){
                            dismiss();
                            Looper.prepare();
                            Toast.makeText(getContext(), "状态已更新", Toast.LENGTH_SHORT).show();
                            Looper.loop();

                        }else {
                            Looper.prepare();
                            Toast.makeText(getContext(), "操作失败", Toast.LENGTH_SHORT).show();
                            Looper.loop();
                        }

                    }
                }).start();
            }
        });
    }
}
